<?php


    class CMail
    {
		var $m_hSocket;		//socket do polaczenia z mail serverem
		
		var $m_iCurrTm;		//obecny czas
			
		var $m_szIp;		//ip serwera smtp
		var $m_iPort;		//port serwera smtp
		
		var $m_szUser;		//login do polaczenia z serwerem pocztowym
		var $m_szPass;		//haslo do polaczenia z serwerem pocztowym
		
		var $m_szFromName; 	//osobnik wysylajacy poczte z nazwy
		var $m_szFromMail;  //mail wysylajacego poczte
		
		static function &GetInstance()
		{
		    global $g_hObjects;
		    static $m_hApp = NULL;
						 
		    if(!isset($m_hApp))
		    {
				$m_hApp = new CMail();
				$g_hObjects[] = $m_hApp;
				$m_hApp->OnInit();	
		    }
		    
		    return $m_hApp;
		}
		
		function OnInit()
		{
			//tworzymy gniazdo
		    $this->m_hSocket = CClientSocket::GetInstance();
		    
		    //obecny czas
		    $this->m_iCurrTm = $_SERVER['REQUEST_TIME'];
		    
		    //dane domyslne
		    $this->SetServer('195.117.130.232','25');
			$this->SetAuth('mateusz.rutkowski@artgen.pl','3enSible');
			$this->SetSender('premiery.pl','mateusz.rutkowski@artgen.pl');
		}
		
		function OnDestroy()
		{
		}
		
		//ustawia serwer smtp
		function SetServer($szIp,$iPort = '25')
		{
			$this->m_szIp  = $szIp;
			$this->m_iPort = $iPort;
		}
		
		//ustawia dane autoryzacyjne 
		function SetAuth($szUser,$szPass)
		{
			$this->m_szUser = base64_encode($szUser);
			$this->m_szPass = base64_encode($szPass);
		}
		
		//ustawia nadawce
		function SetSender($szFromName,$szFromEmail)
		{
			$this->m_szFromName  = $szFromName;
			$this->m_szFromMail = $szFromEmail;
		}
		
		function GetMessageId()
		{
			$szMsgId  = '<';
			$szMsgId .=  date('YmdHis',$this->m_iCurrTm);
			$szMsgId .= '.';
			$szMsgId .= uniqid(rand());
			$szMsgId .= '.';
			$szMsgId .= $this->m_iCurrTm;
			$szMsgId .= '@'.$_SERVER['SERVER_NAME'];
			$szMsgId .= '>';
			
			return $szMsgId;
		}
		
		function GetSendDate()
		{
			return date('D, j M Y H:i:s T',$this->m_iCurrTm);
		}
		
		function Send($szToName,$szToMail,$szTitle,$szBody)
		{
			$this->m_hSocket->Connect(SOCKET_TCP,$this->m_szIp,$this->m_iPort);
		    $szOut = $this->m_hSocket->Read(512);
		    
		 	if(strncmp($szOut,'220',3) != 0) {  return false;  }
		 	
		 	$this->m_hSocket->Write("EHLO localhost\r\n");
		    $szOut = $this->m_hSocket->Read(512);
		    
		    
		    if($this->m_szUser && $this->m_szPass)
		    {
		    	$this->m_hSocket->Write("AUTH LOGIN\r\n");
		    	$szOut = $this->m_hSocket->Read(512);
		    	
		    	$this->m_hSocket->Write($this->m_szUser."\r\n");
		    	$szOut = $this->m_hSocket->Read(512);
		    	
		  		$this->m_hSocket->Write($this->m_szPass."\r\n");
		    	$szOut = $this->m_hSocket->Read(512);
		    }
		    
		    
			$this->m_hSocket->Write("MAIL FROM: <".$this->m_szFromMail.">\r\n");
		    $szOut = $this->m_hSocket->Read(512);
		    
		 	if(strncmp($szOut,'250',3) != 0) { return false; }
		    
		    
			$this->m_hSocket->Write("RCPT TO: <".$szToMail.">\r\n");
		    $szOut = $this->m_hSocket->Read(512);
		    
		    if(strncmp($szOut,'250',3) != 0) { return false; }
		    
		    
		    $this->m_hSocket->Write("DATA \r\n");
		    $szOut = $this->m_hSocket->Read(512);
		    
		    
		    $aHdr['From']          = $this->m_szFromName.' <'.$this->m_szFromMail.'>';
		    $aHdr['To']            = $szToName.' <'.$szToMail.'>';
		    $aHdr['Subject']       = $szTitle;
		    $aHdr['Message-Id']    = $this->GetMessageId();
		    $aHdr['Date']          = $this->GetSendDate();
		    $aHdr['Return-path']   = '<'.$this->m_szFromName.'>';
 		    $aHdr['X-Priority']    = '3';
 			$aHdr['X-UserIP']	   = isset($_SERVER['REMOTE_ADDR'])     ? $_SERVER['REMOTE_ADDR']     : 'Unknown';
 			$aHdr['X-UserBrowser'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Unknown';
 		    
		    
 		    $szData = '';
 		    
 		    foreach($aHdr as $szKey => &$szVal)
 		    {
 		    	$szData.= $szKey.': '.$szVal."\r\n";
 		    }
 		    
 		    $szData .= "MIME-Version: 1.0\r\n";
 		    $szData .= "Content-Transfer-Encoding: 8bit\r\n";
 		    $szData .= "Content-type: text/html; charset=utf-8;\r\n\r\n";
 		    $szData .= $szBody;
 		    
 		    $this->m_hSocket->Write($szData."\r\n.\r\n");
		    $szOutput = $this->m_hSocket->Read(512);
		    
		    $this->m_hSocket->Write("QUIT\r\n");
		}
		
		function SendTpl($szToName,$szToMail,$szTitle,$szTplFile,$aVars = array())
		{
			$szFile = file_get_contents($szTplFile);
			
			foreach($aVars as $szSearch => $szReplace)
		    {
				$szFile = str_replace('{$'.$szSearch.'}',$szReplace,$szFile);
		    }
		    
		    $this->Send($szToName,$szToMail,$szTitle,$szFile);
			
		}
	
    }

?>